#define _LED_MODULE_

#include "common.h"
#include "gpio.h"
#include "ledctr.h"
#include "pulse.h"
#include "dataproc.h"
#include "pulse.h"
#include "os_obj.h"
#include "malloc.h"
#include "iohandle.h"
#include "systask.h"

uint8_t LedBuff[4];
uint32_t LedCnt = 0;
uint8_t databuff[6];
timer_pulse_type led_pulse;
uint8_t *p_LdBuff;

key_type key_st;


/*****************************************************************************/ /*!
*
* @brief   led task.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void LedCtrTask(void *argument)
{
    uint16_t KeyRead;
    
    led_ctr_init();
    pulse_init(&led_pulse, sys_run_st.uTick);
    
    for (;;)
    {
        pulse_create(&led_pulse, sys_run_st.uTick);
        rt_thread_delay(5u);
        if (led_pulse.bPulse_1s)
        {
            LedCnt++;
            Uint16ToAscii(LedCnt/60+1, 3, databuff);

            LedBuff[0] = ascii_code[databuff[0]];
            LedBuff[1] = ascii_code[databuff[1]];
            LedBuff[2] = ascii_code[databuff[2]];
            LedBuff[3] = ascii_code[databuff[3]];
        }
        else if(led_pulse.bPulse_500ms && LedCnt)
        {
            Uint16ToAscii(sys_run_st.sw_tst_cnt/sys_run_st.powerup_time_cnt, 3, databuff);

            LedBuff[0] = ascii_code[databuff[0]];
            LedBuff[1] = ascii_code[databuff[1]]|0x80;
            LedBuff[2] = ascii_code[databuff[2]];
            LedBuff[3] = ascii_code[databuff[3]]; 
        }
        
        KeyRead = REG16_BITS_GET(REG_GPIOF_DATAIN, BITS_MASK(11,14));
        KeyRead = ~KeyRead;
        KeyRead &= BITS_MASK(11,14);
        
        key_st_sample(&key_st, 3, KeyRead);
        key_st_handle(&key_st, led_pulse.bPulse_100ms);
        
        if(key_st.edge)
        {
            if(key_st.lost_edge & BIT_MASK(11))
            {
                DBG_INFO("KEY1 Up\n");
            }
            
            if(key_st.found_edge & BIT_MASK(12))
            {
                DBG_INFO("KEY2 Down\n");
            }
            
            if(key_st.lost_edge & BIT_MASK(13))
            {
                DBG_INFO("KEY3 Up\n");
            }
            
            if(key_st.found_edge & BIT_MASK(14))
            {
                DBG_INFO("KEY4 Down\n");
            } 
            
        }
    }
}



/*****************************************************************************/ /*!
*
* @brief   led init.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void led_ctr_init(void)
{
    outpw(REG_GPIOA_DATAOUT + GPIOH, (0xFFUL << 4 | (0x0FUL << 12)));
    
    LedBuff[0] = ascii_code['0'];
    LedBuff[1] = ascii_code['1'];
    LedBuff[2] = ascii_code['2'];
    LedBuff[3] = ascii_code['3'];
}

/*****************************************************************************/ /*!
*
* @brief   led handle.
*
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void led_ctr_scan(void)
{
    static uint16_t i;
    uint32_t SegMsk;

    outpw(REG_GPIOA_DATAOUT + GPIOH, (0xFFUL << 4 | (0x0FUL << 12)));
    i++;
    i &= 0x03;
    SegMsk = 0x0FUL & (~(1UL << i));
    SegMsk <<= 12;

    outpw(REG_GPIOA_DATAOUT + GPIOH, (((~LedBuff[i]) & 0xFF) << 4) | SegMsk);
}
